home *** CD-ROM | disk | FTP | other *** search
Text File | 1995-04-04 | 11.4 KB | 483 lines | [TEXT/MPCC] |
- /******************** ***********************/
- //
- // Player PRO 4.4x -- OKTA to MADx
- //
- // Version 1.0 - 12.3.95 ANR
- //
- // To use with CodeWarrior 68K or PPC
- //
- // Antoine ROSSET
- // 16 Tranchees
- // 1206 GENEVA
- // SWITZERLAND
- //
- // FAX: (+41 22) 346 11 97
- // Compuserve: 100277,164
- // Internet: rosset@dial.eunet.ch
- //
- /******************** ***********************/
-
- #include "Okta.h"
- #include "MAD.h"
- #include "RDriver.h"
- #include "PPInOut.h"
-
- #if defined(powerc) || defined(__powerc)
- enum {
- PlayerPROPlug = kCStackBased
- | RESULT_SIZE(SIZE_CODE(sizeof(OSErr)))
- | STACK_ROUTINE_PARAMETER(1, SIZE_CODE(sizeof( OSType)))
- | STACK_ROUTINE_PARAMETER(2, SIZE_CODE(sizeof( Ptr*)))
- | STACK_ROUTINE_PARAMETER(3, SIZE_CODE(sizeof( MADPartition*)))
- | STACK_ROUTINE_PARAMETER(4, SIZE_CODE(sizeof( PPInfoRec*)))
- | STACK_ROUTINE_PARAMETER(5, SIZE_CODE(sizeof( short**)))
- };
-
- ProcInfoType __procinfo = PlayerPROPlug;
- #else
- #include <A4Stuff.h>
- #endif
-
- short decode16 (void *msg_buf)
- {
- unsigned char *buf = msg_buf;
- return ( (short) buf[0] << 8) | ( (short) buf[1]);
- }
-
- unsigned long decode32 (void *msg_buf)
- {
- unsigned char *buf = msg_buf;
-
- return( (unsigned long) buf[0] << 24) | ( (unsigned long) buf[1] << 16) | ( (unsigned long) buf[2] << 8) | ( (unsigned long) buf[3]);
- }
-
- short FoundNote( short Period, short *MADpitchTable)
- {
- short NCount = 1;
-
- while( NCount < NUMBER_NOTES+1)
- {
- if( Period >= MADpitchTable[ NCount]) return( NCount);
- NCount++;
- }
-
- return 0;
- }
-
- struct Command* GetCommand( register short PosX, register short TrackIdX, register struct MusicPattern* tempMusicPat)
- {
- if( PosX < 0) PosX = 0;
- else if( PosX >= tempMusicPat->header.PatternSize) PosX = tempMusicPat->header.PatternSize -1;
-
- return( & (tempMusicPat->Commands[ (tempMusicPat->header.PatternSize * TrackIdX) + PosX]));
- }
-
- OSErr ConvertOKTA2Mad( Ptr theOkta, long MODSize, MADPartition *theMAD, short *MADpitchTable)
- {
- short i, PatMax, x, z, channel, TrueTracks;
- long sndSize, OffSetToSample, OldTicks, temp, starting;
- Ptr MaxPtr, theOktaPos;
- OSErr theErr;
- Ptr theInstrument[ 120], destPtr;
- unsigned short tempS;
- char tempChar;
-
-
- /**** Variables pour le MAD ****/
- struct Command *aCmd;
-
- /**** Variables pour le Okta ****/
-
- OktaHeader *Okta;
- OktaInstru *samps, *s, instru[ 120];
- OktaPattern *OktaCmd;
- sectheader *aSect;
- long SectLength;
- short pbod_count, sbod_count;
- /********************************/
-
- for( i = 0 ; i < 64; i ++) theInstrument[ i] = 0L;
-
- theMAD->header = (MADSpec*) NewPtrClear( sizeof( MADSpec));
- Okta = ( struct OktaHeader*) NewPtr( sizeof( struct OktaHeader));
-
- sbod_count = 0;
- pbod_count = 0;
-
- MaxPtr = theOkta + MODSize;
- theOktaPos = theOkta;
-
- if( (*(long*)theOkta) != 'OKTA') DebugStr("\pError in OKTA");
-
- theOktaPos += 8L;
-
- while( theOktaPos < MaxPtr)
- {
- aSect = ( sectheader*) theOktaPos;
- aSect->length = decode32 (&aSect->length);
-
- theOktaPos += 8L;
-
- switch( aSect->name)
- {
- case 'CMOD':
- Okta->splitted[0] = theOktaPos[1];
- Okta->splitted[1] = theOktaPos[3];
- Okta->splitted[2] = theOktaPos[5];
- Okta->splitted[3] = theOktaPos[7];
- Okta->linesize = ((4 +Okta->splitted[0] + Okta->splitted[1]
- + Okta->splitted[2] + Okta->splitted[3])
- * 4);
- break;
-
- case 'SAMP':
- samps = (void*) theOktaPos;
-
- for( i = 0; i * sizeof( OktaInstru) < aSect->length; i++)
- {
- instru[i] = samps[i];
-
- instru[ i ].length = decode32 (&instru[ i ].length);
- instru[ i ].repeat = decode16 (&instru[ i ].repeat) * 2;
- instru[ i ].replen = decode16 (&instru[ i ].replen) * 2;
- }
- Okta->samp_count = i;
- break;
-
- case 'SPEE':
- Okta->speed = decode16( theOktaPos);
- break;
-
- case 'SLEN':
- Okta->slen = decode16( theOktaPos);
- break;
-
- case 'PLEN':
- Okta->plen = decode16( theOktaPos);
- break;
-
- case 'PATT':
- Okta->patt = (void*)theOktaPos;
- break;
-
- case 'PBOD':
- Okta->pbodlen[ pbod_count] = decode16 (theOktaPos);
-
- if( pbod_count == 0) theMAD->header->Tracks = (aSect->length - 2L) / (Okta->pbodlen[ pbod_count] * 4L);
- else
- {
- if( theMAD->header->Tracks != (aSect->length - 2L) / (Okta->pbodlen[ pbod_count] * 4L))
- {
- DebugStr("\pNon-standard OKTA - Tracks");
- }
- }
-
- Okta->pbod[ pbod_count++] = (theOktaPos + 2L);
- break;
-
- case 'SBOD':
- s = &instru[ sbod_count];
-
- if (Okta->splitted[0] || Okta->splitted[1] || Okta->splitted[2] || Okta->splitted[3])
- {
- }
-
- if (s->length < aSect->length) s->length = aSect->length;
-
- theInstrument[ sbod_count] = theOktaPos;
-
- if (s->replen == 0)
- {
- }
- else if (s->repeat != 0)
- {
- }
- else
- {
- }
-
- sbod_count++;
- break;
-
- default:
- DebugStr("\pUnknow section");
- break;
- }
- theOktaPos += aSect->length;
- }
-
- /******** Le Okta a été lu et analysé ***********/
- /******** Copie des informations dans le MAD ***/
-
- theMAD->header->MADIdentification = 'MADF';
- for(i=0; i<32; i++) theMAD->header->NameSignature[i] = 0;
- for(i=0; i<20; i++) theMAD->header->NameSignature[i] = Okta->magic[i];
-
- theMAD->header->PatMax = pbod_count;
- theMAD->header->numPointers = pbod_count;
- for( i = 0; i < 128; i++) theMAD->header->oPointers[ i] = 0;
- for( i = 0; i < pbod_count; i++) theMAD->header->oPointers[ i] = Okta->patt[ i];
-
- for(i=0; i< sbod_count; i++)
- {
- for( x = 0; x < 20; x++) theMAD->header->fid[i].Filename[x] = instru[i].name[ x];
- theMAD->header->fid[i].insSize = instru[i].length;
- theMAD->header->fid[i].fineTune = 0;
- theMAD->header->fid[i].volume = instru[i].vol;
- theMAD->header->fid[i].freq = 1;
- theMAD->header->fid[i].amplitude = 8;
- theMAD->header->fid[i].loopStart = instru[i].repeat;
- theMAD->header->fid[i].loopLenght = instru[i].replen;
-
- if( theMAD->header->fid[i].insSize > 0)
- {
- theMAD->instrument[i] = NewPtr( theMAD->header->fid[i].insSize);
- BlockMove( theInstrument[i], theMAD->instrument[i], theMAD->header->fid[i].insSize);
- }
- else theMAD->instrument[i] = 0L;
- }
-
- for(i = sbod_count; i<64; i++)
- {
- theMAD->instrument[i] = 0L;
- theMAD->header->fid[i].freq = 1;
- theMAD->header->fid[i].amplitude = 8;
- theMAD->header->fid[i].freq = 1;
- }
-
- //*** TEMPORAIRE *****
-
- TrueTracks = theMAD->header->Tracks;
-
- theMAD->header->Tracks /= 2;
- theMAD->header->Tracks *= 2;
- if( theMAD->header->Tracks != TrueTracks) theMAD->header->Tracks += 2;
-
- for(i=0; i<theMAD->header->PatMax; i++)
- {
- theMAD->partition[ i] = (struct MusicPattern*) NewPtrClear( sizeof( struct MusicPattern) + theMAD->header->Tracks * Okta->pbodlen[ i] * sizeof( struct Command));
-
- theMAD->partition[ i]->header.PatternSize = Okta->pbodlen[ i];
- theMAD->partition[ i]->header.CompressionMode = 'NONE';
- for( x = 0; x < 20; x++) theMAD->partition[ i]->header.PatternName[ x] = 0;
- theMAD->partition[ i]->header.PatBytes = 0L;
- theMAD->partition[ i]->header.unused2 = 0L;
-
- MaxPtr = (Ptr) theMAD->partition[ i];
- MaxPtr += sizeof( struct MusicPattern) + theMAD->header->Tracks * Okta->pbodlen[ i] * sizeof( struct Command);
-
- for( x = 0; x < Okta->pbodlen[ i]; x++)
- {
- for(z=0; z<theMAD->header->Tracks; z++)
- {
- aCmd = GetCommand( x, z, theMAD->partition[ i]);
- if( (Ptr) aCmd >= MaxPtr) Debugger();
-
- if( z < TrueTracks)
- {
- OktaCmd = (OktaPattern*) (Okta->pbod[i] + x * TrueTracks * 4L + 4L*z);
-
- if( OktaCmd->b1 > 0)
- {
- aCmd->AmigaPeriod = FoundNote( FreqOktaTable[ OktaCmd->b1 - 1], MADpitchTable);
- aCmd->InstrumentNo = OktaCmd->b2 + 1;
- if( theInstrument[ aCmd->InstrumentNo] == 0L)
- {
- aCmd->InstrumentNo = 0;
- aCmd->AmigaPeriod = 0;
- }
- }
-
- switch( OktaCmd->b3)
- {
- case 31:
- if (OktaCmd->b4 <= 0x40)
- {
- aCmd->EffectCmd = volumeE;
- aCmd->EffectArg = OktaCmd->b4;
- }
- break;
-
- case 25:
- OktaCmd->b4 &= 0xf;
- if( OktaCmd->b4 != 0)
- {
- aCmd->EffectCmd = speedE;
- aCmd->EffectArg = OktaCmd->b4;
- }
- break;
- }
-
- if( i == 0 && x == 0 && z == 0)
- {
- aCmd->EffectCmd = speedE;
- aCmd->EffectArg = Okta->speed;
- }
- }
- else
- {
- aCmd->AmigaPeriod = 0;
- aCmd->InstrumentNo = 0;
- aCmd->EffectCmd = 0;
- aCmd->EffectArg = 0;
- }
- }
- }
- }
-
- DisposPtr( (Ptr) Okta);
-
- return noErr;
- }
-
- void pStrcpy(register unsigned char *s1, register unsigned char *s2)
- {
- register short len, i;
-
- len = *s2;
- for ( i = 0; i <= len; i++) s1[ i] = s2[ i];
- }
-
- OSErr ExtractOKTAInfo( PPInfoRec *info, Ptr AlienFile)
- {
- long PatternSize;
- short i;
- short maxInstru;
- short tracksNo;
-
- /*** Signature ***/
-
- info->signature = 'OKTA';
-
- /*** Internal name ***/
-
- pStrcpy( info->internalFileName, "\p");
-
- /*** Total Patterns ***/
-
- info->totalPatterns = 0;
-
- /*** Partition Length ***/
-
- info->partitionLength = 0;
-
- /*** Total Instruments ***/
-
- info->totalInstruments = 0;
-
- pStrcpy( info->formatDescription, "\pOKTA Plug");
-
- return noErr;
- }
-
- OSErr TestOKTAFile( Ptr AlienFile)
- {
- long *myOKTA = ( long*) AlienFile;
-
- if( *myOKTA == 'OKTA') return noErr;
- else return fileNotSupportedByThisPlug;
- }
-
- OSErr main( OSType order, FSSpec *AlienFileFSSpec, MADPartition *MadFile, PPInfoRec *info, short *MADpitchTable)
- {
- OSErr myErr;
- Ptr AlienFile;
- short vRefNum, iFileRefI;
- long dirID, sndSize;
-
- #ifndef powerc
- long oldA4 = SetCurrentA4(); //this call is necessary for strings in 68k code resources
- #endif
-
- HGetVol( 0L, &vRefNum, &dirID);
- HSetVol( 0L, AlienFileFSSpec->vRefNum, AlienFileFSSpec->parID);
-
- myErr = noErr;
-
- switch( order)
- {
- case 'IMPL':
- myErr = FSOpen( AlienFileFSSpec->name, 0, &iFileRefI);
- if( myErr == noErr)
- {
- GetEOF( iFileRefI, &sndSize);
-
- // ** MEMORY Test Start
- AlienFile = NewPtr( sndSize * 2L);
- if( AlienFile == 0L) myErr = needMoreMemory;
- // ** MEMORY Test End
-
- else
- {
- DisposPtr( AlienFile);
-
- AlienFile = NewPtr( sndSize);
- myErr = FSRead( iFileRefI, &sndSize, AlienFile);
- if( myErr == noErr)
- {
- myErr = TestOKTAFile( AlienFile);
- if( myErr == noErr)
- {
- myErr = ConvertOKTA2Mad( AlienFile, GetPtrSize( AlienFile), MadFile, MADpitchTable);
- }
- }
- DisposPtr( AlienFile); AlienFile = 0L;
- }
- FSClose( iFileRefI);
- }
- break;
-
- case 'TEST':
- myErr = FSOpen( AlienFileFSSpec->name, 0, &iFileRefI);
- if( myErr == noErr)
- {
- sndSize = 1024L;
-
- AlienFile = NewPtr( sndSize);
- if( AlienFile == 0L) myErr = needMoreMemory;
- else
- {
- myErr = FSRead( iFileRefI, &sndSize, AlienFile);
- myErr = TestOKTAFile( AlienFile);
-
- DisposPtr( AlienFile); AlienFile = 0L;
- }
- FSClose( iFileRefI);
- }
- break;
-
- case 'INFO':
- myErr = FSOpen( AlienFileFSSpec->name, 0, &iFileRefI);
- if( myErr == noErr)
- {
- GetEOF( iFileRefI, &info->fileSize);
-
- sndSize = 5000L; // Read only 5000 first bytes for optimisation
-
- AlienFile = NewPtr( sndSize);
- if( AlienFile == 0L) myErr = needMoreMemory;
- else
- {
- myErr = FSRead( iFileRefI, &sndSize, AlienFile);
- if( myErr == noErr)
- {
- myErr = ExtractOKTAInfo( info, AlienFile);
- }
- DisposPtr( AlienFile); AlienFile = 0L;
- }
- FSClose( iFileRefI);
- }
- break;
-
- default:
- myErr = orderNotImplemented;
- break;
- }
-
- HSetVol( 0L, vRefNum, dirID);
-
- #ifndef powerc
- SetA4( oldA4);
- #endif
- return myErr;
- }